<?php
/**
 * 新闻
 * User: Lin Hui
 * Date: 2017/5/22
 * Time: 11:44
 */

namespace Home\Controller;


use Common\Model\ArticleModel;

class NewsController extends BaseController
{
    public $pageTheme = "<ul class='pageList clear'><li>%UP_PAGE%</li>%FIRST% %LINK_PAGE% %END%<li>%DOWN_PAGE%</li></ul>";
    /**
     * 企业新闻
     */
    public function company()
    {
        $category = C('ARTICLE.category');
        list($list, $page) = (new ArticleModel())->getArticleByCategory($category['company'], 6);
        $page->rollPage = 5;
        $page->setConfig('theme',$this->pageTheme);
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');

        $this->assign('page', $page->show());
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 媒体新闻
     */
    public function media()
    {
        $category = C('ARTICLE.category');
        list($list, $page) = (new ArticleModel())->getArticleByCategory($category['media'], 6);
        $page->rollPage = 5;
        $page->setConfig('theme',$this->pageTheme);
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');

        $this->assign('page', $page->show());
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 视频新闻
     */
    public function video()
    {
        $category = C('ARTICLE.category');
        list($list, $page) = (new ArticleModel())->getArticleByCategory($category['video'], 9);
        $page->rollPage = 5;
        $page->setConfig('theme',$this->pageTheme);
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $this->assign('page', $page->show());
        $this->assign('list', $list);
        $this->display();
    }



    /**
     * 新闻详情页
     */
    public function detail()
    {
        $id = I('id', 0, 'intval');
        $cat = I('cat');
        $model = new ArticleModel();
        $info = $model->getInfo($id);
        if($info){
            if(!$cat) {
                $cat = $model->getCategory($id);
            }
            $category = C('ARTICLE.category');
            $category_id = $category[$cat];
            if($category_id) {
                $up = $model->getUpArticle($category_id, $info['create_time']);
                $down = $model->getDownArticle($category_id, $info['create_time']);
                $this->assign('up',$up);
                $this->assign('down', $down);
            }
            vendor("phpqrcode.phpqrcode");
            // 纠错级别：L、M、Q、H
            $level = 'L';
            // 点的大小：1到10,
            $size = 7;
            $fileName = './Uploads/'.microtime().rand(100,999).'.png';
            \QRcode::png(currentUrl(), $fileName, $level, $size, 0);

            $this->assign('cat', $cat);
            $this->assign('qrcode', base64EncodeImage($fileName));
            unlink($fileName);
            $this->assign('recommend', $model->getRecommend(5));
            $this->assign('info', $info);
            $this->display();
        } else {
            $this->redirect('Index/index');
        }

    }

    /**
     * 搜索结果页
     */
    public function search()
    {
        $map = [];
        if($keywords = I('keywords')) {
            $map['title'] = ['like', "%$keywords%"];
        }
        $model = new ArticleModel();
        list($list, $page) = $model->getArticleByMap($map, 6);
        foreach($list as $key => $item) {
            $list[$key]['title'] = wrapWord(more_hidden($item['title'], 0, 24), $keywords, ['<span class="searchKeyText">', '</span>']);
        }
        $page->rollPage = 5;
        $page->setConfig('theme', $this->pageTheme);
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }
}